<?php
/**
 * > アイテムデータ
 *
 * LICENSE: Miracle Maple by Mirage is licensed
 * under a Creative Commons 表示-非営利-継承 2.1 日本 License.
 *
 * @category   item
 * @package    maple-story.jp
 * @version    8.0.0
 * @link       http://maple-story.jp/item/
 * @since      File available since Release 8.0.0
 * @copyright  Copyright 2011 Miracle Maple
 * @license    http://creativecommons.org/licenses/by-nc-sa/2.1/jp/     Creative Commons BY-NC-SA
 */
class ItemController extends Zend_Controller_Action
{
	protected $_item;
	
	public function init()
	{
		$this->_item = new Model_TrnItem();
	}
	
    /**
     * トップ
     */
     public function indexAction()
    {
		if ($this->_getParam('name') == '') {
			return $this->_forward('start');					   
		}
		
        $this->_rowsItem = $this->_item->getDataByName($this->_getParam('name'));
		
		if (count($this->_rowsItem) == 1) {
			return $this->_forward('detail', 'item', 'default', array('name' => $this->_getParam('name')));
		} else if (count($this->_rowsItem) > 1) {
			return $this->_forward('list', 'item', 'default', array('name' => $this->_getParam('name')));	
		} else {
			throw new Zend_Controller_Action_Exception('No data was found in Item database.');
		}
    }
	
	public function startAction()
	{
		
	}
    
    public function listAction()
    {
        $rowsItem = $this->_item->getDataByName($this->_getParam('name'));
		
		if (!is_array($rowsItem)) {
			throw new Zend_Controller_Action_Exception('Invalid Data Model has provided in item list.');
		}
		
		if ($this->_getParam('index') != 0) {
			return $this->_forward('detail', 'item', 'default',
							       array('name'  => $this->_getParam('name'),
								         'index' => $this->_getParam('index')
										 )
								   );
		}
		
		$this->view->items = $rowsItem;
    }
    
    public function detailAction()
    {
        $rowsItem = $this->_item->getDataByName($this->_getParam('name'));
		
		if ($this->_getParam('index') != 0) {
			$index = $this->_getParam('index') - 1;
		} else {
			$index = 0;
		}
		
		if ($index > count($rowsItem)) {
			throw new Zend_Controller_Action_Exception('Index number which has provided was out of range.');
		} else if (!$rowsItem[$index] instanceof Model_TrnItem) {
			throw new Zend_Controller_Action_Exception('Invalid Data Model has provided in item detail.');
		}
		
		$rowItem = $rowsItem[$index];
		
		$this->view->item = $rowItem;
		$this->view->headTitle('アイテム', 'PREPEND');
		$this->view->headTitle($rowItem->name, 'PREPEND');
		
		// パンくず設定
        $topicPath = array();
        $topicPath[] = array('name' => 'アイテム', 'url' => '/アイテム/');
        $topicPath[] = array('name' => $rowItem->name);
		$this->view->topicPath = $topicPath;
	
		if (count($rowsItem) == 1) {
			$this->view->headMeta()->appendName('canonical', '/アイテム/' . $rowItem->name . '/');
		} else {
			$this->view->headMeta()->appendName('canonical', '/アイテム/' . $rowItem->name . '/' . $index . '/');
		}
	}
	
	public function searchAction()
	{
		$rowsItem = $this->_item->search($this->getRequest()->getParams());

		if (count($rowsItem) == 1) {
			return $this->_forward('detail', 'item', 'default', array('name' => $rowsItem[0]->name));
		}
			
		$this->view->items = $rowsItem;
		$this->view->searchOptions = $this->getRequest()->getParams();
	}
        
}
